package cc.fs.study.guideview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cc.fs.study.R;

//导航view
public class GuideView extends View {
    private final String TAG = "GuideView Log";
    //背景颜色
    private int mColor = Color.BLACK;
    //当前提示位置
    private int mPosition = 0;
    //需要显示的位置
    private List<View> viewList = new ArrayList<>();
    //绘画位置
    private Rect drawRect = new Rect();
    private Rect tipRect = new Rect();
    private Paint paint = new Paint();
    private PorterDuffXfermode mode;
    private Bitmap arrowBitmap;
    private int textColor = 0xFF45BB9C;
    private float textSize = 50f;

    public GuideView(Context context) {
        this(context, null);
    }

    public GuideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GuideView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //关闭硬件加速
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mode = new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT);
        arrowBitmap = ((BitmapDrawable) ContextCompat.getDrawable(getContext(), R.mipmap.ic_arrow)).getBitmap();
        paint.setColor(mColor);
        paint.setAntiAlias(true);
        paint.setAlpha(150);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setXfermode(null);
        paint.setColor(mColor);
        paint.setAlpha(150);
        canvas.drawCircle((drawRect.left + drawRect.right) / 2, (drawRect.top + drawRect.bottom) / 2, (drawRect.top + drawRect.bottom) / 3, paint);
        paint.setXfermode(mode);
        canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
        paint.setXfermode(null);
        canvas.drawBitmap(arrowBitmap, tipRect.left, tipRect.top, paint);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        canvas.drawText("功能", tipRect.left, tipRect.bottom + (paint.getFontMetrics().bottom - paint.getFontMetrics().top) / 2, paint);
    }

    private void setCurrentTip(int pos) {
        if (viewList != null && pos < viewList.size()) {
            //记录最新展示的位置
            final View mView = viewList.get(mPosition);
            mView.post(new Runnable() {
                @Override
                public void run() {
                    drawRect.left = ((View) mView.getParent()).getLeft() + mView.getLeft();
                    drawRect.right = mView.getRight();
                    drawRect.top = ((View) mView.getParent()).getTop() + mView.getTop();
                    drawRect.bottom = ((View) mView.getParent()).getTop() + mView.getBottom();

                    int mWidth = drawRect.right - drawRect.left;
                    int bWidth = arrowBitmap.getWidth();
                    if (bWidth <= mWidth) {
                        tipRect.left = drawRect.left + (mWidth - bWidth) / 2;
                    } else {
                        tipRect.left = drawRect.left - (bWidth - mWidth) / 2;
                    }

                    tipRect.top = drawRect.bottom + 50;
                    tipRect.right = tipRect.left + arrowBitmap.getWidth();
                    tipRect.bottom = tipRect.top + arrowBitmap.getHeight();

                    //更新页面
                    postInvalidate();
                }
            });
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //处理点击事件
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (mPosition < viewList.size()) mPosition++;
            else mPosition = 0;
            setCurrentTip(mPosition);
        }
        return true;
    }

    public void setViewList(List<View> viewList) {
        this.viewList.addAll(viewList);
        setCurrentTip(0);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //销毁view
        try {
            if (arrowBitmap != null) {
                arrowBitmap.recycle();
            }
        } catch (Exception e) {
            Log.w("TAG", " recycle GuideView arrowBitmap faild");
        }
    }
}
